package com.example.demo.util;

import com.example.demo.Constant;
import org.springframework.data.redis.core.RedisTemplate;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author ListJiang
 * @since 2021/10/22
 * description 自定义token工具类
 */
public class TokenUtil {

    public static String getToken(HttpServletRequest request, HttpServletResponse response) {
        String authorization = request.getHeader(Constant.TOKEN);
        if (request.getCookies() != null) {
            for (Cookie cookie : request.getCookies()) {
                if (Constant.TOKEN.equals(cookie.getName())) {
                    authorization = cookie.getValue();
                    cookie.setMaxAge(30 * 60);
                    response.addCookie(cookie);
                }
            }
        }
        return authorization;
    }
}